package com.mylg.craw.house.government.category;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Getter
@Setter
@ToString
public class PreSellStatistics {
    private static final Pattern PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]*批准预售面积(\\d+\\.\\d+)万平方米");

    //总数
    private String total;

    //住宅
    private String residence;

    //非住宅
    private String noResidence;

    private int endIndex;

    public static PreSellStatistics parse(String text) {
        // 创建匹配器
        PreSellStatistics statistics = new PreSellStatistics();
        Matcher matcher = PATTERN.matcher(text);
        // 如果找到匹配，则输出匹配的文本
        while (matcher.find()) {
            String content = matcher.group(0);
            statistics.endIndex = Math.max(statistics.endIndex, text.indexOf(content) + content.length());
            String number = matcher.group(1);
            if (content.contains("商品房批准预售")) {
                statistics.setTotal(number);
            } else if (content.contains("商品住宅")) {
                statistics.setResidence(number);
            } else if (content.contains("非住宅")) {
                statistics.setNoResidence(number);
            }
        }
        return statistics;
    }
}
